package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.exception.HandException;
import com.javaee.term.model.SaleMan;
import com.javaee.term.service.SalemanService;
import com.javaee.term.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/17 0017 19:08
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/saleman")
@Slf4j
public class SaleManController {

    @Autowired
    SalemanService salemanService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

        PageHelper.startPage(page,size);
        List<SaleMan> list=salemanService.findAll();
        PageInfo<SaleMan> SaleMansInfoPage=new PageInfo<SaleMan>(list);
        map.put("SaleMansInfoPage", SaleMansInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", SaleMansInfoPage.getPages());
        return new ModelAndView("saleman/list", map);
    }

    @GetMapping("/delete")
    public ModelAndView list(@RequestParam(value = "salemanId")  String salemanId,
                             Map<String, Object> map) {
        try {
            Integer conver=Integer.parseInt(salemanId);
            salemanService.delete(conver);
        } catch (HandException e) {
            log.error("【后台端删除用户】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/saleman/list");
            return new ModelAndView("common/error", map);
        }

        map.put("msg", "操作成功");
        map.put("url", "/sell/seller/saleman/list");
        return new ModelAndView("common/success",map);
    }
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "salemanId", required = false) Integer salemanId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(salemanId)) {
            SaleMan saleManInfo = salemanService.findOne(salemanId);
            map.put("saleManInfo", saleManInfo);
        }

        return new ModelAndView("saleman/index", map);
    }
    @PostMapping("/save")
    public ModelAndView save(@Valid SaleMan form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/saleman/index");
            return new ModelAndView("common/error", map);
        }

        SaleMan saleMan = new SaleMan();
        try {
            //如果manId为空, 说明是新增
            if (!StringUtils.isEmpty(form.getManId())) {
                saleMan = salemanService.findOne(form.getManId());
                BeanUtils.copyProperties(form, saleMan);
                salemanService.update(saleMan);
            } else {
                form.setManId(KeyUtil.getUniqueKey());
                BeanUtils.copyProperties(form, saleMan);
                try {
                    salemanService.insert(saleMan);
                }
                catch (HandException e) {
                    map.put("msg", e.getMessage());
                    map.put("url", "/sell/seller/saleman/list");
                    return new ModelAndView("common/error", map);
                }
            }

        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/saleman/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/saleman/list");
        return new ModelAndView("common/success", map);
    }

}
